<?php

namespace App\Repository\Repositories;
use App\Helpers\Image;
use App\Http\Controllers\Controller;
use App\Models\StoreProductReply;
use App\Repository\RepositoryInterface\ShopInterface;
class ShopRepositories extends Controller implements ShopInterface
{
    protected $shop;

    public function __construct(StoreProductReply $productReply)
    {
        $this->shop = $productReply;
    }

    //*** why
    /**
     * @return \Illuminate\Http\JsonResponse
     * 查询评论
     */
    public function shopRemarkAll()
    {
        try {
            $data = $this->shop->with(['goods','user'])->paginate(5);
            if (!$data){
                return parent::error('查找失败');
            }
            return parent::success('查找成功',$data);
        }catch (\Exception $e){
            return parent::error($e->getMessage());
        }
    }

    /**
     * @param $data
     * @return \Illuminate\Http\JsonResponse
     * 添加评论
     */
    public function shopRemarkAdd($data)
    {
        try {
            $result = $this->shop::create($data);
            if (!$result){
                return parent::error('添加失败');
            }
            return parent::success('添加成功');
        }catch (\Exception $e){
            return parent::error($e->getMessage());
        }
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * 删除评论
     */
    public function shopRemarkDel($id)
    {
        try {
            $result = $this->shop::where('id',$id)->delete();

            if (!$result){
                return parent::error('删除失败');
            }
            return parent::success('删除成功');
        }catch (\Exception $e){
            return parent::error($e->getMessage());
        }
    }
    public function shopRemarkEdit($data)
    {
        try {
            $result = $this->shop::update($data);
            if (!$result){
                return parent::error('修改失败');
            }
            return parent::success('修改成功');
        }catch (\Exception $e){
            return parent::error($e->getMessage());
            }
    }
    /**
     * @param $data
     * @return \Illuminate\Http\JsonResponse
     * 搜索评论
     */
    public function shopRemarkSearch($data)
    {
        try {
            $result = $this->shop::where('comment','like',"$data%")->get();

            if (!$result){
                return parent::error('搜索失败');
            }
            return ['code'=>200,'msg'=>'搜索成功','data'=>$data];
        }catch (\Exception $e){
            return parent::error($e->getMessage());
        }
    }

    /**
     * @param $file
     * @return false|\Illuminate\Http\JsonResponse|string
     * 上传评论图片
     */
    public function remarkImageUpload($file)
    {
        try {
            $ak = 'gYTXWzI0T90vqwS0tZWe5OXZuP8eMhlXYx7cqkDg';
            $sk = 'sa9WOQFYdnP1NjFRpHYnVVCrTydJnY-jBjW-J_MX';
            $bucket = 'whytest1';
            $url = 'http://why.wyt.plus/';
            //七牛云上传
            $imageUrl = (new Image())->uploadFile($file,$ak,$sk,$bucket,$url);
            return $imageUrl;
        }catch (\Exception $e){
            return parent::error($e->getMessage());
        }
    }

}
